package com.wan.fragment


import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v7.widget.LinearLayoutManager
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup

import com.wan.R
import com.wan.activity.WebActivity
import com.wan.adapter.NavigationFlowAdapter
import com.wan.adapter.NavigationRvAdapter
import com.wan.base.BaseBean
import com.wan.entity.HomeArtItemEntity
import com.wan.entity.NavigationArtEntity
import com.wan.uitls.APIServices
import com.wan.uitls.CommonUtils
import com.wan.uitls.FlowItemClickListener
import com.wan.uitls.ItemClickListener
import kotlinx.android.synthetic.main.fragment_navigation.*
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory


/**
 * 导航
 *
 */
class NavigationFragment : Fragment(), ItemClickListener, FlowItemClickListener {

    private var lastClick = 0

    private val TAG = this.javaClass.name

    private var dataList = ArrayList<NavigationArtEntity>()
    private lateinit var rvAdapter: NavigationRvAdapter

    private var flowList = ArrayList<HomeArtItemEntity>()
    private lateinit var flowAdapter: NavigationFlowAdapter
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_navigation, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        flowAdapter = NavigationFlowAdapter(context!!, flowList, this)
        navigation_flow_fy.adapter = flowAdapter

        navigation_rv.layoutManager = LinearLayoutManager(context)
        rvAdapter = NavigationRvAdapter(context!!, dataList, R.layout.fragment_navigation_rv_item)
        navigation_rv.adapter = rvAdapter

        rvAdapter.setItemClickListener(this)

        initNetWork()
    }

    private fun initNetWork() {
        var retrofit = Retrofit.Builder()
                .baseUrl(APIServices.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build()
        var apiServices = retrofit.create(APIServices::class.java)
        apiServices.getNavigationItemList().enqueue(object : Callback<BaseBean<ArrayList<NavigationArtEntity>>> {
            override fun onFailure(call: Call<BaseBean<ArrayList<NavigationArtEntity>>>, t: Throwable) {
                Log.e(TAG, "失败了")
            }

            override fun onResponse(call: Call<BaseBean<ArrayList<NavigationArtEntity>>>, response: Response<BaseBean<ArrayList<NavigationArtEntity>>>) {
                Log.e(TAG, "成功了")
                var data = response.body()!!.data
                dataList.addAll(data)
                rvAdapter.notifyDataSetChanged()
                flowList.addAll(data[0].articles)
                flowAdapter.notifyDataChanged()
            }
        })
    }

    override fun onFlowItemClickListener(position: Int) {
        CommonUtils.toastWord(context!!,"123")
        var entity = flowList[position]
        CommonUtils.sendIntent(context!!, WebActivity::class.java, entity.link, entity.title)
    }

    override fun onItemClickListener(position: Int) {
        if (position == lastClick) {
            return
        } else {
            var arrayList = dataList[position].articles
            flowList.clear()
            flowList.addAll(arrayList)
            flowAdapter.notifyDataChanged()
        }
    }



}
